/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.valstorm.mb;

import br.valstorm.facade.ProjectFacade;
import br.valstorm.model.Project;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

/**
 *
 * @author grunitzki
 */
@ManagedBean
@SessionScoped
public class ProjectMB extends AbstractMB implements Serializable {

    @EJB
    private ProjectFacade projectFacade;
    
    private Project project = new Project();
    private List<Project> projects;
    
    @PostConstruct
    private void init(){
        this.project = new Project();
    }
    
    public void cadNewProject(ActionEvent actionEvent){
         if (project.getId() == 0) {
            try {
                closeDialog();
                projectFacade.save(project);
                project = new Project();
                displayInfoMessageToUser("Projeto cadastrado com sucesso");
            }catch(Exception e){
                displayErrorMessageToUser("Erro ao cadastrar projeto");
            }

        } else {
            project = projectFacade.update(project);
            project = new Project();
        }
    }
    
    public void clear() {
        project = new Project();
    }

    public void remove() {
        projectFacade.delete(project);
        project = new Project();
    }

    public void loadProjects(ActionEvent event) {
        System.out.println("finding projects");
        projects = projectFacade.findAll();
        System.out.println("Quantidade: " + projects.size());
    }
    
    public void teste(){
        TreeNode node = new DefaultTreeNode();
    }

    public ProjectFacade getProjectFacade() {
        return projectFacade;
    }

    public void setProjectFacade(ProjectFacade projectFacade) {
        this.projectFacade = projectFacade;
    }

    public Project getProject() {
        return project;
    }

    public void setProject(Project project) {
        this.project = project;
    }

    public List<Project> getProjects() {
        return projects;
    }

    public void setProjects(List<Project> projects) {
        this.projects = projects;
    }    
}
